<?php
class Console {
	
	const LEVEL_INFO = 1;
	const LEVEL_ERROR = 2;
	const LEVEL_DEBUG = 3;
	const LEVEL_TRACE = 4;
	
	private static $instance = null;
	
	private $logs;
	private $level;
	private $dumps;
	
	private $db;
	
	public function __construct() {
		if (self::$instance != null) return;
		self::$instance = $this;
		
		$CI = &get_instance();
		$this->logs = array();
		$this->dumps = array();
		$this->level = $CI->config->item('log_level');
		
		if (!$CI->config->item('is_debug')) {
			$this->level = 0;
		}
		
		$this->db = &$CI->db;
	}
	
	public static function info($str) {
		if (self::$instance->level >= self::LEVEL_INFO)
			self::$instance->log('info', $str);
	}
	
	public static function error($str) {
		if (self::$instance->level >= self::LEVEL_ERROR)
			self::$instance->log('error', $str);
	}
	
	public static function debug($str) {
		if (self::$instance->level >= self::LEVEL_DEBUG)
			self::$instance->log('debug', $str);
	}
	
	public static function trace($str) {
		if (self::$instance->level >= self::LEVEL_TRACE)
			self::$instance->log('trace', $str);
	}
	
	public static function isDebugEnabled() {
		return self::$instance->level >= self::LEVEL_DEBUG;
	}
	
	public static function isTraceEnabled() {
		return self::$instance->level >= self::LEVEL_TRACE;
	}
	
	public static function var_dump($var) {
		if (self::$instance->level > 0)
			self::$instance->dumps[] = $var;
	}
	
	public static function get_dumps() {
		return self::$instance->dumps;
	}
	
	public static function get_logs() {
		return self::$instance->logs;
	}
	
	public static function get_database_data() {
		$data = array(
				'driver' => self::$instance->db->dbdriver,
				'username' => self::$instance->db->username,
				'hostname' => self::$instance->db->hostname,
				'database' => self::$instance->db->database,
				'char_set' => self::$instance->db->char_set,
				'dbcollat' => self::$instance->db->dbcollat,
				'queries' => self::$instance->db->queries,
				'query_times' => self::$instance->db->query_times,
				'data_cache' => self::$instance->db->data_cache
				
		);
		
		return $data;
	}
	
	private function log($type, $str) {
		$this->logs[] = array('level' => $type, 'log' => $str);
	}
}